<template>
  <div>
    <!-- 工作日历, 年和月 -->
    <div class="select-box">
      <el-select v-model="currentYear" size="small" style="width: 120px; margin-right: 10px" @change="handleChange">
        <!-- 年份取给定年份的, 前五年 + 后五年 -->
        <el-option v-for="item in yearList" :key="item" :label="item" :value="item" />
      </el-select>
      <el-select v-model="currentMonth" size="small" style="width: 120px;" @change="handleChange">
        <el-option v-for="item in 12" :key="item" :label="item" :value="item" />
      </el-select>
    </div>
    <!-- 日历组件 -->
    <el-calendar v-model="currentDate">
      <template #dateCell="{date,data}">
        <!-- date:  Wed Jun 01 2022 08:00:00 GMT+0800 (中国标准时间) -->
        <!-- data:{ "isSelected": true, "type": "current-month", "day": "2022-06-01" } -->
        <!-- 组件内部<slot name="dateCell" :date="xxx" :data="xxx"></slot> -->
        <div class="date-content">
          <span class="text">{{ data.day |time }}</span>
          <span v-if="isWeekend(date)" class="rest">休</span>
        </div>
      </template>
    </el-calendar>
  </div>
</template>

<script>
export default {
  filters: {
    time(value) {
      const val = value.split('-')[2] // 01 02 03 15 16 17
      return val.startsWith('0') ? val.slice(1) : val
    }
  },
  props: {
    startDate: {
      // 开始日期(复杂类型的props  如果提供默认值,需要通过函数return)
      type: Date,
      default: () => {
        return new Date()
      }
    }
  },
  data() {
    return {
      currentYear: null, // 当前年份
      currentMonth: null, // 当前月份
      // yearList: [] // 要遍历的年的数组
      currentDate: null// null 找不到会拿当前时间(new Date())
    }
  },
  computed: {
    yearList() {
      // 根据this.currentYear动态计算出一个数组,前五年  当前年份 后五年
      // forEach
      // 数组的长度永远都是11
      return Array.from(new Array(11)).map((item, index) => index + this.currentYear - 5)
      // const list = []
      // return Array.from(new Array(11)).forEach((item, index) => {
      //   list.push(index + this.currentYear - 5)
      //   return list
      // })
    }
  },
  watch: {
    currentDate(newval) {
      this.currentYear = newval.getFullYear(
        this.currentMonth = newval.getMonth() + 1
      )
    }
  },
  created() {
    this.currentYear = this.startDate.getFullYear() // 得到当前年份
    this.currentMonth = this.startDate.getMonth() + 1 // 当前月份
    this.handleChange() // 一进入,就更新当前日历
  },
  methods: {
    handleChange() {
      // 在用户选择了年份 月份 同步日历结构
      this.currentDate = new Date(`${this.currentYear}-${this.currentMonth}-01`)
    },
    isWeekend(date) {
      // 如果发现是周六周日,返回true,反之 返回false
      if (date.getDay() === 0 || date.getDay() === 6) {
        return true
      }
      return false
    }
  }
}
</script>

<style lang="scss" scoped>
.select-box {
  display: flex;
  justify-content: flex-end;
}

::v-deep .el-calendar-day {
  height:  auto;
 }
::v-deep .el-calendar-table__row td::v-deep .el-calendar-table tr td:first-child, ::v-deep .el-calendar-table__row td.prev{
  border: none;
 }
.date-content {
  height: 40px;
  text-align: center;
  line-height: 40px;
  font-size: 14px;
}
.date-content .rest {
  color: #fff;
  border-radius: 50%;
  background: rgb(250, 124, 77);
  width: 20px;
  height: 20px;
  line-height: 20px;
  display: inline-block;
  font-size: 12px;
  margin-left: 10px;
}
.date-content .text{
  width: 20px;
  height: 20px;
  line-height: 20px;
 display: inline-block;

}
::v-deep .el-calendar-table td.is-selected .text{
   background: #409eff;
   color: #fff;
   border-radius: 50%;
 }
::v-deep .el-calendar__header {
   display: none
}
</style>
